import {request, handleFetchResultNotification, REQUEST_TYPE, formatDateTime} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import * as dto from './DTO';
const baseUrl = '/parts/api/v1';

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '查询初始化数据'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询备件信息'
});
const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});

const freezeDesc = formatMessage({
    id: 'api.freeze',
    defaultMessage: '批量冻结'
});

const thawDesc = formatMessage({
    id: 'api.thaw',
    defaultMessage: '批量解冻'
});

const exportBasicDataDesc = formatMessage({
    id: 'api.exportBasicDataDesc',
    defaultMessage: '导出备件信息'
});
const salesAttributesImportTempDesc = formatMessage({
    id: 'api.salesAttributesImportTempDesc',
    defaultMessage: '下载导入销售属性模板'
});
const checkAttributesImportTempDesc = formatMessage({
    id: 'api.checkAttributesImportTempDesc',
    defaultMessage: '下载导入检查属性模板'
});
const produceAttributesImportTempDesc = formatMessage({
    id: 'api.produceAttributesImportTempDesc',
    defaultMessage: '下载导入生产信息模板'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改信息'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/part/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));
// 查询备件
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts?${queryString}`, null, dto.ListData)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询备件详情
export const getPartsDetail = id => request(`${baseUrl}/parts/${id}`, null, dto.DetailData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 导出备件信息
export const exportBasicData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/exportBasicData?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportBasicDataDesc));
};

export const exportCheckedPopertiesData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/exportCheckedPopertiesData?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportBasicDataDesc));
};

export const exportSalesAttributesPopertiesData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/exportSalesAttributesPopertiesData?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportBasicDataDesc));
};

export const exportPartSalesPriceData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/exportPartSalesPriceData?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportBasicDataDesc));
};

// 冻结
export const freezeApplyReports = data => request(`${baseUrl}/parts/freeze`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, freezeDesc));

// 解冻
export const thawApplyReports = data => request(`${baseUrl}/parts/thaw`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, thawDesc));

// 导入销售属性模板
export const salesAttributesImportTemp = () => request(`${baseUrl}/parts/salesAttributes/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, salesAttributesImportTempDesc)).then(res => res.data);

// 导入检查属性模板
export const checkAttributesImportTemp = () => request(`${baseUrl}/parts/checkAttributes/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, checkAttributesImportTempDesc)).then(res => res.data);
// 导入生产信息模板
export const produceAttributesImportTemp = () => request(`${baseUrl}/parts/produceAttributes/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, produceAttributesImportTempDesc)).then(res => res.data);

export const updateData = (id, data) => request(`${baseUrl}/parts/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
